package cn.zwz.home.controller;

import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.home.entity.StudentClass;
import cn.zwz.home.service.IStudentClassService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 郑为中
 */
@Slf4j
@RestController
@Api(tags = "班级管理接口")
@RequestMapping("/zwz/studentClass")
@Transactional
public class StudentClassController {

    @Autowired
    private IStudentClassService iStudentClassService;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条班级")
    public Result<StudentClass> get(@RequestParam String id){
        return new ResultUtil<StudentClass>().setData(iStudentClassService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部班级个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iStudentClassService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部班级")
    public Result<List<StudentClass>> getAll(){
        return new ResultUtil<List<StudentClass>>().setData(iStudentClassService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询班级")
    public Result<IPage<StudentClass>> getByPage(@ModelAttribute StudentClass studentClass ,@ModelAttribute PageVo page){
        QueryWrapper<StudentClass> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(studentClass.getTitle())) {
            qw.like("title",studentClass.getTitle());
        }
        if(!ZwzNullUtils.isNull(studentClass.getLevel())) {
            qw.eq("level",studentClass.getLevel());
        }
        IPage<StudentClass> data = iStudentClassService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<StudentClass>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改班级")
    public Result<StudentClass> saveOrUpdate(StudentClass studentClass){
        if(iStudentClassService.saveOrUpdate(studentClass)){
            return new ResultUtil<StudentClass>().setData(studentClass);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增班级")
    public Result<StudentClass> insert(StudentClass studentClass){
        iStudentClassService.saveOrUpdate(studentClass);
        return new ResultUtil<StudentClass>().setData(studentClass);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑班级")
    public Result<StudentClass> update(StudentClass studentClass){
        iStudentClassService.saveOrUpdate(studentClass);
        return new ResultUtil<StudentClass>().setData(studentClass);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除班级")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iStudentClassService.removeById(id);
        }
        return ResultUtil.success();
    }
}
